package com.ayoka.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.ayoka.utils.WeChatManager;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		try {
			WeChatManager.mWxApi.handleIntent(getIntent(), this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		finish();
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		if (WeChatManager.mWxApi != null) {
			WeChatManager.mWxApi.handleIntent(getIntent(), this);
		}
		finish();
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
		switch (resp.errCode) {
			case BaseResp.ErrCode.ERR_OK:
				if (ConstantsAPI.COMMAND_PAY_BY_WX == resp.getType()) {
//                    ("微信支付成功");
					WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WE_CHAT_PAY_SUCCESS);
				}
				break;
			case BaseResp.ErrCode.ERR_SENT_FAILED:
				if (ConstantsAPI.COMMAND_PAY_BY_WX == resp.getType()) {
//                    ("微信支付失败");
					WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WE_CHAT_PAY_FILED);
				}
				break;
			case BaseResp.ErrCode.ERR_AUTH_DENIED:
			case BaseResp.ErrCode.ERR_USER_CANCEL:
				if (ConstantsAPI.COMMAND_PAY_BY_WX == resp.getType()) {
//                    ("用户取消支付");
					WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WE_CHAT_PAY_CANCEL);
				}
				break;
			default:
				WeChatManager.getInstance().notifyWeChatOperation(WeChatManager.WE_CHAT_PAY_FILED);
				break;
		}
		finish();
	}
}